/**
 * Malsha 24/07/2014
 */

ChangePassword = new Object();

Ext.apply(Ext.form.field.VTypes, {
	password : function(val, field) {
		if (field.initialPassField) {
			var pwd = field.up('form').down('#' + field.initialPassField);
			return (val == pwd.getValue());
		}
		return true;
	},
	passwordText : 'Passwords do not match',
});

ChangePassword.getCenterPanel = function() {

	var formpanel_cahange_password = null;
	formpanel_cahange_password = Ext.create('Ext.form.Panel', {
		title : 'Change Password',
		bodyPadding : 20,
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 110,
			anchor : '65%',
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'User',
			allowBlank : false,
			anchor : '40%',
			name : 'userName',
			readOnly : true,
			msgTarget : 'under',
			value : App.LoginUser
		}, {
			xtype : 'textfield',
			fieldLabel : 'Current Password',
			name : 'oldPassword',
			inputType : 'password',
			allowBlank : false,
			msgTarget : 'under',
			anchor : '40%',
		}, {
			xtype : 'textfield',
			fieldLabel : 'New Password',
			name : 'pass',
			inputType : 'password',
			id : 'pass',
			msgTarget : 'under',
			allowBlank : false,
			anchor : '40%',
			listeners : {
				focus : function(field, The, eOpts) {
					Ext.getCmp('re_pass').setValue('');
				}
			}
		}, {
			xtype : 'textfield',
			fieldLabel : 'Confirm New Password',
			vtype : 'password',
			msgTarget : 'under',
			inputType : 'password',
			allowBlank : false,
			initialPassField : 'pass',
			anchor : '40%',
			id : 're_pass',
			name : 'newPassword'
		} ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					formpanel_cahange_password.setLoading('Updating...');
					form.url = 'changePassword.action';
					form.submit({
						success : function(form, action) {
							form.reset();
							Ext.Msg.alert('Success', 'Changed Password Successfully');
							formpanel_cahange_password.setLoading(false);
						},
						failure : function(form, action) {
							if (action.result.resultMsg == "2") {
								Ext.Msg.alert('Failed', 'Current Password Dose not match');
							} else {
								Ext.Msg.alert('Failed', 'User unavailable');
							}
							formpanel_cahange_password.setLoading(false);
						}
					});
				}
			}
		} ]
	});
	return formpanel_cahange_password;
};